package com.icode.web.controller.house;

import com.icode.core.dto.house.MyHouseDTO;
import com.icode.core.dto.house.RentDTO;
import com.icode.core.dto.house.RentInitDTO;
import com.icode.core.model.house.UploadImage;
import com.icode.core.model.user.User;
import com.icode.core.model.user.UserInfo;
import com.icode.core.shared.SecurityUtil;
import com.icode.service.house.MyHouseService;
import com.icode.service.management.RentService;
import com.icode.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-9-16
 * Time: 上午10:40
 * To change this template use File | Settings | File Templates.
 */
@RequestMapping("/my_house")
@Controller("MyHouseController")
public class MyHouseController {

    @Autowired
    private MyHouseService myHouseService;

    @Autowired
    private UserService userService;

    @Autowired
    private RentService rentService;

    @RequestMapping(value = "show1", method = RequestMethod.GET)
    private String showMyHouse1(Model model) {
        User user = SecurityUtil.currentUser();
        List<MyHouseDTO> myHouseDTOs = myHouseService.findMyHouse(user);
        List<MyHouseDTO> myChangeHouses = myHouseService.findMyChangeHouse(user);
        model.addAttribute("myHouseDTOs", myHouseDTOs);
        model.addAttribute("myChangeHouses", myChangeHouses);
        return "front/user/myHouse";
    }


    @RequestMapping(value = "show", method = RequestMethod.GET)
    private String showMyHouse(Model model) {
        User user = SecurityUtil.currentUser();
        UserInfo userInfoByUser = userService.findUserInfoByUser(user);
        UploadImage uploadImage = userInfoByUser.usersInfoImage();
        String guid = null;
        if (uploadImage != null) {
            guid = uploadImage.guid();
        }
        model.addAttribute("userImageGuid", guid);
        return "front/user/myHouseNew";
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    private String showMyHouseList(Model model) {
        User user = SecurityUtil.currentUser();
        List<MyHouseDTO> myHouseDTOs = myHouseService.findMyHouse(user);
        model.addAttribute("myHouseDTOs", myHouseDTOs);
        model.addAttribute("userImageGuid", user.userInfo().usersInfoImage().guid());
        return "front/user/myHouse_list";
    }

    @RequestMapping(value = "exchang", method = RequestMethod.GET)
    private String showMyHouseExchang(Model model) {
        User user = SecurityUtil.currentUser();
        List<MyHouseDTO> myChangeHouses = myHouseService.findMyChangeHouse(user);
        model.addAttribute("myHouseDTOs", myChangeHouses);
        UploadImage uploadImage = user.userInfo().usersInfoImage();
        String imageGuid = uploadImage == null ? "" : uploadImage.guid();
        model.addAttribute("userImageGuid", imageGuid);
        return "front/user/myHouse_exchang";
    }

    @RequestMapping(value = "transfer", method = RequestMethod.GET)
    private String showMyHouseTransfer(Model model) {
        User user = SecurityUtil.currentUser();
        List<MyHouseDTO> myTransferHouses = myHouseService.findMyTransferHouses(user);
        model.addAttribute("myHouseDTOs", myTransferHouses);
        model.addAttribute("userImageGuid", user.userInfo().usersInfoImage().guid());
        return "front/user/myHouse_transfer";
    }

    @RequestMapping(value = "toassignmner/{myHouseGuid}", method = RequestMethod.GET)
    private String toAssignmner(@PathVariable String myHouseGuid) {
        myHouseService.myHouserToAssigmnent(myHouseGuid);
        return "redirect:/toAssignmnet/list";
    }

    @RequestMapping(value = "rent/hirer", method = RequestMethod.GET)
    public String myRentHirer(Model model) {
        User user = SecurityUtil.currentUser();
        List<RentDTO> rentDTOs = rentService.findRentForHirer(user);
        model.addAttribute("rentDTOs", rentDTOs);
        return "front/user/myHouse_rent";
    }

    @RequestMapping(value = "rent/lessee", method = RequestMethod.GET)
    public String myRentLessee(Model model) {
        User user = SecurityUtil.currentUser();
        List<RentDTO> rentDTOs = rentService.findRentForLessee(user);
        model.addAttribute("rentDTOs", rentDTOs);
        return "front/user/myHouse_rent";
    }


    @RequestMapping(value = "init/rent/{myHouseGuid}/{price}/{days}/{description}")
    public String initRent(@PathVariable String myHouseGuid, @PathVariable String price, @PathVariable String days, @PathVariable String description) {
        myHouseService.initRent(myHouseGuid, price, days, description);
        return "redirect:/rent/list/HOUSEBASE";
    }
}
